<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Format\HonorDisgracesFormat;
use App\Http\Requests\HonorDisgracesCreateRequest;
use App\Http\Requests\HonorDisgracesListRequest;
use App\Services\HonorDisgracesService;
use Illuminate\Http\Request;

class HonorController extends Controller
{
    protected $honorDisgracesService;

    public function __construct(HonorDisgracesService $honorDisgracesService)
    {
        $this->honorDisgracesService = $honorDisgracesService;
    }

    public function getLists(HonorDisgracesListRequest $honorDisgracesListRequest)
    {
        $page = $honorDisgracesListRequest->input('page', 1);
        $pageSize = $honorDisgracesListRequest->input('page_size', 10);
        $orderColumn = $honorDisgracesListRequest->input('order_column', '');
        $orderType = $honorDisgracesListRequest->input('order_type', '');
        $search = [];
        if ($honorDisgracesListRequest->input('name')) {
            $search['name'] = $honorDisgracesListRequest->input('name');
        }
        if ($honorDisgracesListRequest->input('start_date')) {
            $search['start_date'] = $honorDisgracesListRequest->input('start_date');
        }
        if ($honorDisgracesListRequest->input('end_date')) {
            $search['end_date'] = $honorDisgracesListRequest->input('end_date');
        }
        if ($honorDisgracesListRequest->input('type')) {
            $search['type'] = $honorDisgracesListRequest->input('type');
        }
        $res = $this->honorDisgracesService->getList($page, $pageSize, $orderColumn, $orderType, $search);
        return renderSuccessJson($res);
    }

    public function craete(HonorDisgracesCreateRequest $honorDisgracesCreateRequest)
    {
        $format = new HonorDisgracesFormat($honorDisgracesCreateRequest->all());
        $this->honorDisgracesService->create($format);
        return renderSuccessJson();
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $detail = $this->honorDisgracesService->detail($id);
        return renderSuccessJson($detail);
    }

    public function update(Request $request)
    {
        $format = new HonorDisgracesFormat($request->all());
        $this->honorDisgracesService->update($format);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->honorDisgracesService->delete($id);
        return renderSuccessJson();
    }
}
